﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.Outbound
{
    public partial class OutboundDetailEdit : System.Web.UI.Page
    {
        public string ID
        {
            set
            {
                ViewState["ID"] = value;
            }
            get
            {
                return ViewState["ID"] != null ? ViewState["ID"].ToString() : Guid.NewGuid().ToString();
            }
        }

        public string FlowInstanceID
        {
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
            get
            {
                return ViewState["FlowInstanceID"] != null ? ViewState["FlowInstanceID"].ToString() : string.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ID = Request.QueryString["id"];
                this.FlowInstanceID = Request.QueryString["FlowInstanceID"];
            }
        }        

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.FlowInstanceID))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
                return;
            }

            try
            {
                UpdateData();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>");
            }
        }

        private void UpdateData()
        {
            string sql_select = @"SELECT COUNT(ID) FROM dbo.t5715_OutboundDetail WHERE ID = @ID";
            string sql_insert = @"INSERT INTO dbo.t5715_OutboundDetail
                                       (ID
                                        ,FlowInstanceID
                                        ,ItemCode
                                        ,ItemName
                                        ,PartCode
                                        ,PartName
                                        ,ProductCount
                                        ,Remark)
                                 VALUES
                                       (@ID
                                        ,@FlowInstanceID
                                        ,@ItemCode
                                        ,@ItemName
                                        ,@PartCode
                                        ,@PartName
                                        ,@ProductCount
                                        ,@Remark)";

            string sql_update = @"UPDATE dbo.t5715_OutboundDetail
                                   SET FlowInstanceID = @FlowInstanceID
                                        ,ItemCode = @ItemCode
                                        ,ItemName = @ItemName
                                        ,PartCode = @PartCode
                                        ,PartName = @PartName
                                        ,ProductCount = @ProductCount
                                        ,Remark = @Remark
                                 WHERE ID = @ID";

            try
            {

                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_select);
                database.AddInParameter(dbCommand, "@ID", DbType.String, this.ID);
                string count = database.ExecuteScalar(dbCommand).ToString();

                if (count.Equals("0"))
                {
                    dbCommand = database.GetSqlStringCommand(sql_insert);
                }
                else
                {
                    dbCommand = database.GetSqlStringCommand(sql_update);
                }

                database.AddInParameter(dbCommand, "@ID", DbType.String, this.ID);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, this.FlowInstanceID);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, hidItemCode.Value);
                database.AddInParameter(dbCommand, "@ItemName", DbType.String, hidItemName.Value);
                database.AddInParameter(dbCommand, "@PartCode", DbType.String, hidPartCode.Value);
                database.AddInParameter(dbCommand, "@PartName", DbType.String, hidPartName.Value);
                database.AddInParameter(dbCommand, "@ProductCount", DbType.String, txtCount.Text);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, txtRemark.Text);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
        }
    }
}